package org.njo.webapp.root.model.activity;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.njo.webapp.root.model.dao.ActionRoleMappingDAO;
import org.njo.webapp.root.utility.SystemContext;

public class SecurityActivity {
    /**
     * Log Object.
     */
    private static Log logger = LogFactory
            .getLog(SecurityActivity.class);
    
    
    public List getActionRoleMappingList() {
        Connection connection = null;
        ActionRoleMappingDAO actionRoleMappingDAO = null;
        List result = null;
        // access the database.
        try {
            // open the database connection.
            connection = SystemContext.openConnection();
            actionRoleMappingDAO = new ActionRoleMappingDAO(connection);
            result = new ArrayList();
            
            List actionPathList = actionRoleMappingDAO.selectActionPaths(1, Integer.MAX_VALUE);
            
            for (int i = 0; i < actionPathList.size(); i++) {
                String actionPath = (String)actionPathList.get(i);
                List actionRoleList =  actionRoleMappingDAO.selectActionRoles(actionPath);
                
                Map actionMapping = new HashMap();
                actionMapping.put("path", actionPath);
                actionMapping.put("roles", actionRoleList.toArray(new String[]{}));
                result.add(actionMapping);
            }
            return result;
        } catch (SQLException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new RuntimeException(ex);
        } finally {
            try {
                if (connection != null) {
                    SystemContext.closeConnection(connection);
                }
            } catch (Throwable tw) {
            }
        }
    }    
    
    
}
